/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.core.analyzer;


import org.open.cobertura.data.CoberturaCoverageCounter;

/**
 * 覆盖率计算接口
 */
public abstract class CoberturaCoveredImpl implements ICoberturaCoveredNode{
    /**
     * 行的偏移量，一般是从类开始计算
     */
    protected int offset;
    protected CoberturaCoverageCounter instructionCounter;
    protected CoberturaCoverageCounter branchCounter;
    protected CoberturaCoverageCounter lineCounter;
    protected CoberturaCoverageCounter methodCounter;



    @Override
    public CoberturaCoverageCounter getInstructionCounter() {
        return instructionCounter;
    }

    @Override
    public CoberturaCoverageCounter getBranchCounter() {
        return branchCounter;
    }

    @Override
    public CoberturaCoverageCounter getLineCounter() {
        return lineCounter;
    }

    @Override
    public CoberturaCoverageCounter getComplexityCounter() {
        return new CoberturaCoverageCounter();
    }



    @Override
    public CoberturaCoverageCounter getMethodCounter() {
        return methodCounter;
    }


    @Override
    public abstract int getFirstLine();

    @Override
    public abstract int getLastLine();

    @Override
    public abstract CoberturaLineCovered getLine(int row);


    private void increaseInstru(CoberturaCoverageCounter instructionCount){
        this.instructionCounter.coveredCount+=instructionCount.coveredCount;
        this.instructionCounter.missedCount+=instructionCount.missedCount;
        this.instructionCounter.totalCount=instructionCounter.coveredCount+instructionCounter.missedCount;
    }
    private void increaseBranch(CoberturaCoverageCounter branchCount){
        this.branchCounter.coveredCount+=branchCount.coveredCount;
        this.branchCounter.missedCount+=branchCount.missedCount;
        this.branchCounter.totalCount=this.branchCounter.coveredCount+this.branchCounter.missedCount;
    }

    protected void increaseCoveredLine(int covered){
        this.lineCounter.coveredCount += covered;
        this.lineCounter.setTotalCount(this.lineCounter.coveredCount+this.lineCounter.missedCount);
    }

    protected void increaseMissedLine(int missed){
        this.lineCounter.missedCount += missed;
        this.lineCounter.setTotalCount(this.lineCounter.coveredCount+this.lineCounter.missedCount);
    }
    /**
     * 累加指令覆盖率
     * @param instructionCount
     * @param branchCount
     */
    protected void increase(CoberturaCoverageCounter instructionCount,CoberturaCoverageCounter branchCount){
        this.increaseInstru(instructionCount);
        this.increaseBranch(branchCount);

    }

    /**
     * 累加行覆盖
     * @param instructionCount
     * @param branchCount
     * @return
     */
    public CoberturaLineCovered increaseLine(CoberturaCoverageCounter instructionCount,CoberturaCoverageCounter branchCount){
        CoberturaLineCovered coberturaLineCovered=new CoberturaLineCovered(instructionCount.getMissedCount(),instructionCount.getCoveredCount(),branchCount.getMissedCount(),branchCount.getCoveredCount());
        coberturaLineCovered.instructionCounter.setTotalCount(instructionCount.getMissedCount()+instructionCount.getCoveredCount());
        coberturaLineCovered.branchCounter.setTotalCount(branchCount.getMissedCount()+branchCount.getCoveredCount());
        return coberturaLineCovered;
    }
}
